package com.hz.controller;

import com.hz.pojo.Article;
import com.hz.pojo.PageBean;
import com.hz.pojo.Result;
import com.hz.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    @GetMapping("/list")
    public Result getList(){
        return Result.success("所有文章的列表。。。。");
    }

    @PostMapping
    public Result addArticle(@RequestBody @Validated(Article.add.class) Article article){
        return articleService.addArticle(article)==1?Result.success("新增成功"):Result.error("新增失败");
    }

    @GetMapping
    public Result<PageBean<Article>> listArticle(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ){

        PageBean<Article> pb = articleService.list(pageNum,pageSize,categoryId,state);
        return Result.success(pb);
    }

    @PutMapping
    public Result updateArticle(@RequestBody @Validated(Article.update.class) Article article){
        return articleService.updateArticle(article)==1?Result.success():Result.error("更新失败");

    }

    @DeleteMapping()
    public Result deleteArticle(Integer id){
        return articleService.deleteArticle(id)==1?Result.success():Result.error("删除失败");
    }

    @GetMapping("/detail")
    public Result<Article> getDetail(@RequestParam(required = true) Integer id){
        Article article = articleService.getDetail(id);
        return Result.success(article);
    }


}
